package com.kajespero.domain.site.impl;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.Date;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.kajespero.domain.article.ArticleDomain;
import com.kajespero.domain.article.impl.ArticleDomainImpl;
import com.kajespero.domain.page.PageDomain;
import com.kajespero.domain.page.impl.PageDomainImpl;
import com.kajespero.domain.site.SiteDomain;
import com.kajespero.domain.summary.SummaryDomain;
import com.kajespero.domain.summary.impl.SummaryDomainImpl;
import com.kajespero.domain.template.TemplateDomain;
import com.kajespero.domain.template.impl.TemplateDomainImpl;
import com.kajespero.enums.TemplateType;
import com.kajespero.model.ArticleBean;
import com.kajespero.model.PageBean;
import com.kajespero.model.SummaryBean;
import com.kajespero.model.TemplateBean;
import com.kajespero.model.UserBean;
import com.kajespero.vo.UserVO;

public class SiteDomainImpl implements SiteDomain{
	
	private static final Log LOG = LogFactory.getLog(SiteDomainImpl.class);
	
	private static final SummaryDomain SUMMARY_DOMAIN = new SummaryDomainImpl();
	
	private static final PageDomain PAGE_DOMAIN = new PageDomainImpl();
	
	private static final TemplateDomain TEMPLATE_DOMAIN = new TemplateDomainImpl();
	
	private static final ArticleDomain ARTICLE_DOMAIN = new ArticleDomainImpl();

	@Override
	public boolean initSite(UserVO creator) {
		SummaryBean administration = SUMMARY_DOMAIN.createNewBean();
		administration.setTitle("Administration");
		administration.setUrl("/administration");
		
		SUMMARY_DOMAIN.saveOrUpdade(administration);
		
		TemplateBean templateAdmin = TEMPLATE_DOMAIN.createNewBean();
		templateAdmin.setTitle("Administration-Template");
		templateAdmin.setType(TemplateType.VELOCITY);
		
		URL urlTemplate = getClass().getResource("/templates/administration.vm");
		
		try {
			InputStream inputStream = FileUtils.openInputStream(new File(urlTemplate.toURI()));
			templateAdmin.setValue(IOUtils.toString(inputStream));
			
		} catch (IOException e) {
			templateAdmin.setValue("try again");
			LOG.error("IOException " + e.getMessage(), e);
		} catch (URISyntaxException e) {
			LOG.error("URISyntaxException " + e.getMessage(), e);
		}
		
		TEMPLATE_DOMAIN.saveOrUpdade(templateAdmin);
		
		
		
		PageBean pageBean = PAGE_DOMAIN.createNewBean();
		pageBean.setSummary(administration);
		pageBean.setTitle("Web Site Administration");
		pageBean.setTemplate(templateAdmin);
		
		PAGE_DOMAIN.saveOrUpdade(pageBean);
		
		TemplateBean templatePage = TEMPLATE_DOMAIN.createNewBean();
		templatePage.setTitle("Page-Template");
		templatePage.setType(TemplateType.VELOCITY);
		
		urlTemplate = getClass().getResource("/templates/page.vm");
		
		try {
			InputStream inputStream = FileUtils.openInputStream(new File(urlTemplate.toURI()));
			templatePage.setValue(IOUtils.toString(inputStream));
			
		} catch (IOException e) {
			LOG.error("IOException " + e.getMessage(), e);
		} catch (URISyntaxException e) {
			LOG.error("URISyntaxException " + e.getMessage(), e);
		}
		
		TEMPLATE_DOMAIN.saveOrUpdade(templatePage);
		
		createIndexSite(creator);
		return true;
	}
	
	private void createIndexSite(UserVO creator){
		TemplateBean cssTemplate = TEMPLATE_DOMAIN.createNewBean();
		cssTemplate.setTitle("CSS-TEMPLATE");
		cssTemplate.setType(TemplateType.CSS);
		
		URL urlTemplate = getClass().getResource("/templates/main.css");
		
		try {
			InputStream inputStream = FileUtils.openInputStream(new File(urlTemplate.toURI()));
			cssTemplate.setValue(IOUtils.toString(inputStream));
			
		} catch (IOException e) {
			LOG.error("IOException " + e.getMessage(), e);
		} catch (URISyntaxException e) {
			LOG.error("URISyntaxException " + e.getMessage(), e);
		}
		
		TEMPLATE_DOMAIN.saveOrUpdade(cssTemplate);
		
		SummaryBean mainSummary = SUMMARY_DOMAIN.createNewBean();
		mainSummary.setTitle("Main");
		mainSummary.setUrl("/");
		
		SUMMARY_DOMAIN.saveOrUpdade(mainSummary);
		
		PageBean page = PAGE_DOMAIN.createNewBean();
		page.setSummary(mainSummary);
		page.setTemplate(cssTemplate);
		page.setTitle("Welcome Page");
		
		PAGE_DOMAIN.saveOrUpdade(page);
		
		ArticleBean article = ARTICLE_DOMAIN.createNewBean();
		article.setTitle("My First Article");
		article.setCreateAt(new Date());
		article.setCreateBy((UserBean) creator);
		article.setPage(page);
		article.setText("<p>This article had been create by the system<p><p>Enjoy and Make yourself your web site</p> ");
		
		ARTICLE_DOMAIN.saveOrUpdade(article);
		
	}

}
